{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建一个Series,同时让pandas自动生成索引列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = pd.Series([1,3,5,np.nan,6,8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1.0\n",
       "1    3.0\n",
       "2    5.0\n",
       "3    NaN\n",
       "4    6.0\n",
       "5    8.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看s\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建一个DataFrame数据框"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 创建一个DataFrame ，可以传入一个numpy array 可以自己构建索引以及列标\n",
    "dates = pd.date_range('2018-11-01',periods=7)\n",
    "#### 比如说生成一个时间序列，以20181101 为起始位置的，7个日期组成的时间序列，数据的类型为datetime64[ns]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2018-11-01', '2018-11-02', '2018-11-03', '2018-11-04',\n",
       "               '2018-11-05', '2018-11-06', '2018-11-07'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>-1.913573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>0.712624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2018-11-01 -0.170364 -0.237541  0.529903  0.660073\n",
       "2018-11-02 -0.158446 -0.488535  0.082960 -1.913573\n",
       "2018-11-03 -0.518426  0.730866 -1.033830  0.712624\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208\n",
       "2018-11-06 -0.030580  0.545561  1.091127 -0.131579\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(7,4),index= dates,columns=list('ABCD'))\n",
    "df\n",
    "# 产生随机正态分布的数据，7行4列，分别对应的index的长度以及column的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>20181101</td>\n",
       "      <td>3</td>\n",
       "      <td>test</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>20181101</td>\n",
       "      <td>3</td>\n",
       "      <td>train</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>20181101</td>\n",
       "      <td>3</td>\n",
       "      <td>test</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>20181101</td>\n",
       "      <td>3</td>\n",
       "      <td>train</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A         B  C      D    E\n",
       "0  1  20181101  3   test  1.5\n",
       "1  1  20181101  3  train  1.5\n",
       "2  1  20181101  3   test  1.5\n",
       "3  1  20181101  3  train  1.5"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 同时用可以使用dict的实行创建DataFrame\n",
    "df2 = pd.DataFrame({\"A\":1,\n",
    "                   \"B\":\"20181101\",\n",
    "                   'C':np.array([3]*4,dtype='int32'),\n",
    "                   'D':pd.Categorical(['test','train','test','train']),\n",
    "                   \"E\":1.5},\n",
    "                  )\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A       int64\n",
       "B      object\n",
       "C       int32\n",
       "D    category\n",
       "E     float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.dtypes\n",
    "### 查看数据框中的数据类型,常见的数据类型还有时间类型以及float类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查看数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>-1.913573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>0.712624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2018-11-01 -0.170364 -0.237541  0.529903  0.660073\n",
       "2018-11-02 -0.158446 -0.488535  0.082960 -1.913573\n",
       "2018-11-03 -0.518426  0.730866 -1.033830  0.712624\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "# 比如说看前5行\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208\n",
       "2018-11-06 -0.030580  0.545561  1.091127 -0.131579\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 后4行\n",
    "df.tail(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2018-11-01', '2018-11-02', '2018-11-03', '2018-11-04',\n",
       "               '2018-11-05', '2018-11-06', '2018-11-07'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看DataFrame的索引\n",
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['A', 'B', 'C', 'D'], dtype='object')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看DataFrame的列索引\n",
    "df.columns\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.1703643 , -0.23754121,  0.52990284,  0.66007285],\n",
       "       [-0.15844565, -0.48853537,  0.08296043, -1.91357255],\n",
       "       [-0.51842554,  0.73086567, -1.03382969,  0.71262388],\n",
       "       [ 1.01352712,  0.27016714,  0.08180539,  0.17819344],\n",
       "       [-0.89749689, -0.01627937, -0.23499323,  0.08120819],\n",
       "       [-0.03058032,  0.54556063,  1.09112723, -0.13157934],\n",
       "       [-0.31334198, -0.68817881, -0.41775393,  0.85502652]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看DataFrame的数据,将DataFrame转化为numpy array 的数据形式\n",
    "df.values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据的简单统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>7.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>7.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>-0.153590</td>\n",
       "      <td>0.016580</td>\n",
       "      <td>0.014174</td>\n",
       "      <td>0.063139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.590144</td>\n",
       "      <td>0.527860</td>\n",
       "      <td>0.680939</td>\n",
       "      <td>0.945526</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>-1.913573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-0.415884</td>\n",
       "      <td>-0.363038</td>\n",
       "      <td>-0.326374</td>\n",
       "      <td>-0.025186</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>-0.094513</td>\n",
       "      <td>0.407864</td>\n",
       "      <td>0.306432</td>\n",
       "      <td>0.686348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>0.855027</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              A         B         C         D\n",
       "count  7.000000  7.000000  7.000000  7.000000\n",
       "mean  -0.153590  0.016580  0.014174  0.063139\n",
       "std    0.590144  0.527860  0.680939  0.945526\n",
       "min   -0.897497 -0.688179 -1.033830 -1.913573\n",
       "25%   -0.415884 -0.363038 -0.326374 -0.025186\n",
       "50%   -0.170364 -0.016279  0.081805  0.178193\n",
       "75%   -0.094513  0.407864  0.306432  0.686348\n",
       "max    1.013527  0.730866  1.091127  0.855027"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可以使用describe函数对DataFrame中的数值型数据进行统计\n",
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>C</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         A    C    E\n",
       "count  4.0  4.0  4.0\n",
       "mean   1.0  3.0  1.5\n",
       "std    0.0  0.0  0.0\n",
       "min    1.0  3.0  1.5\n",
       "25%    1.0  3.0  1.5\n",
       "50%    1.0  3.0  1.5\n",
       "75%    1.0  3.0  1.5\n",
       "max    1.0  3.0  1.5"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.describe()\n",
    "### 对于其他的数据类型的数据describe函数会自动过滤掉"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2018-11-01 00:00:00</th>\n",
       "      <th>2018-11-02 00:00:00</th>\n",
       "      <th>2018-11-03 00:00:00</th>\n",
       "      <th>2018-11-04 00:00:00</th>\n",
       "      <th>2018-11-05 00:00:00</th>\n",
       "      <th>2018-11-06 00:00:00</th>\n",
       "      <th>2018-11-07 00:00:00</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.518426</td>\n",
       "      <td>1.013527</td>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.030580</td>\n",
       "      <td>-0.313342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.237541</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>-0.688179</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.417754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>0.660073</td>\n",
       "      <td>-1.913573</td>\n",
       "      <td>0.712624</td>\n",
       "      <td>0.178193</td>\n",
       "      <td>0.081208</td>\n",
       "      <td>-0.131579</td>\n",
       "      <td>0.855027</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   2018-11-01  2018-11-02  2018-11-03  2018-11-04  2018-11-05  2018-11-06  \\\n",
       "A   -0.170364   -0.158446   -0.518426    1.013527   -0.897497   -0.030580   \n",
       "B   -0.237541   -0.488535    0.730866    0.270167   -0.016279    0.545561   \n",
       "C    0.529903    0.082960   -1.033830    0.081805   -0.234993    1.091127   \n",
       "D    0.660073   -1.913573    0.712624    0.178193    0.081208   -0.131579   \n",
       "\n",
       "   2018-11-07  \n",
       "A   -0.313342  \n",
       "B   -0.688179  \n",
       "C   -0.417754  \n",
       "D    0.855027  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### DataFrame 的转置，将列索引与行索引进行调换，行数据与列数进行调换\n",
    "df.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>-1.913573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>0.712624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2018-11-01 -0.170364 -0.237541  0.529903  0.660073\n",
       "2018-11-02 -0.158446 -0.488535  0.082960 -1.913573\n",
       "2018-11-03 -0.518426  0.730866 -1.033830  0.712624\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208\n",
       "2018-11-06 -0.030580  0.545561  1.091127 -0.131579\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据的排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>0.712624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>-1.913573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027\n",
       "2018-11-06 -0.030580  0.545561  1.091127 -0.131579\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193\n",
       "2018-11-03 -0.518426  0.730866 -1.033830  0.712624\n",
       "2018-11-02 -0.158446 -0.488535  0.082960 -1.913573\n",
       "2018-11-01 -0.170364 -0.237541  0.529903  0.660073"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_index(ascending=False)\n",
    "### 降序，按照列进行降序，通过该索引列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   A         B         C         D\n",
      "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027\n",
      "2018-11-02 -0.158446 -0.488535  0.082960 -1.913573\n",
      "2018-11-01 -0.170364 -0.237541  0.529903  0.660073\n",
      "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208\n",
      "2018-11-04  1.013527  0.270167  0.081805  0.178193\n",
      "2018-11-06 -0.030580  0.545561  1.091127 -0.131579\n",
      "2018-11-03 -0.518426  0.730866 -1.033830  0.712624\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>-1.913573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>0.712624</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027\n",
       "2018-11-02 -0.158446 -0.488535  0.082960 -1.913573\n",
       "2018-11-01 -0.170364 -0.237541  0.529903  0.660073\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193\n",
       "2018-11-06 -0.030580  0.545561  1.091127 -0.131579\n",
       "2018-11-03 -0.518426  0.730866 -1.033830  0.712624"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "print(df.sort_values(by=['B','A']))\n",
    "#  默认是升序,可以选择多指排序，先照B，后排A，如果B中的数据一样，则按照A中的大小进行排序\n",
    "df.sort_values(by='B')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 选择数据（类似于数据库中sql语句）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2018-11-01   -0.170364\n",
       "2018-11-02   -0.158446\n",
       "2018-11-03   -0.518426\n",
       "2018-11-04    1.013527\n",
       "2018-11-05   -0.897497\n",
       "2018-11-06   -0.030580\n",
       "2018-11-07   -0.313342\n",
       "Freq: D, Name: A, dtype: float64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['A']\n",
    "# 取出单独的一列数据，等价于df.A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>-1.913573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>0.712624</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2018-11-01 -0.170364 -0.237541  0.529903  0.660073\n",
       "2018-11-02 -0.158446 -0.488535  0.082960 -1.913573\n",
       "2018-11-03 -0.518426  0.730866 -1.033830  0.712624"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过[]进行行选择切片\n",
    "df[0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>-1.913573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>0.712624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2018-11-01 -0.170364 -0.237541  0.529903  0.660073\n",
       "2018-11-02 -0.158446 -0.488535  0.082960 -1.913573\n",
       "2018-11-03 -0.518426  0.730866 -1.033830  0.712624\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 同时对于时间索引而言，可以直接使用比如\n",
    "df['2018-11-01':'2018-11-04']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 另外可以使用标签来选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A   -0.170364\n",
       "B   -0.237541\n",
       "C    0.529903\n",
       "D    0.660073\n",
       "Name: 2018-11-01 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "df.loc['2018-11-01']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B\n",
       "2018-11-01 -0.170364 -0.237541\n",
       "2018-11-02 -0.158446 -0.488535\n",
       "2018-11-03 -0.518426  0.730866\n",
       "2018-11-04  1.013527  0.270167\n",
       "2018-11-05 -0.897497 -0.016279\n",
       "2018-11-06 -0.030580  0.545561\n",
       "2018-11-07 -0.313342 -0.688179"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#### 通过标签来进行多个轴上的进行选择\n",
    "df.loc[:,[\"A\",\"B\"]] # 等价于df[[\"A\",\"B\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B\n",
       "2018-11-01 -0.170364 -0.237541\n",
       "2018-11-02 -0.158446 -0.488535\n",
       "2018-11-03 -0.518426  0.730866"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[\"2018-11-01\":\"2018-11-03\",[\"A\",\"B\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.17036430076617162"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#### 获得一个标量数据\n",
    "df.loc['2018-11-01','A']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 通过位置获取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    1.013527\n",
       "B    0.270167\n",
       "C    0.081805\n",
       "D    0.178193\n",
       "Name: 2018-11-04 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[3]  # 获得第四行的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.08296</td>\n",
       "      <td>-1.913573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.03383</td>\n",
       "      <td>0.712624</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   B        C         D\n",
       "2018-11-02 -0.488535  0.08296 -1.913573\n",
       "2018-11-03  0.730866 -1.03383  0.712624"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[1:3,1:4]  #  与numpy中的ndarray类似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.488535</td>\n",
       "      <td>-1.913573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.178193</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   B         D\n",
       "2018-11-02 -0.488535 -1.913573\n",
       "2018-11-04  0.270167  0.178193"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 可以选取不连续的行或者列进行取值\n",
    "df.iloc[[1,3],[1,3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.08296</td>\n",
       "      <td>-1.913573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.03383</td>\n",
       "      <td>0.712624</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B        C         D\n",
       "2018-11-02 -0.158446 -0.488535  0.08296 -1.913573\n",
       "2018-11-03 -0.518426  0.730866 -1.03383  0.712624"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  对行进行切片处理\n",
    "df.iloc[1:3,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>-1.913573</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>0.712624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   B         C         D\n",
       "2018-11-01 -0.237541  0.529903  0.660073\n",
       "2018-11-02 -0.488535  0.082960 -1.913573\n",
       "2018-11-03  0.730866 -1.033830  0.712624\n",
       "2018-11-04  0.270167  0.081805  0.178193\n",
       "2018-11-05 -0.016279 -0.234993  0.081208\n",
       "2018-11-06  0.545561  1.091127 -0.131579\n",
       "2018-11-07 -0.688179 -0.417754  0.855027"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对列进行切片\n",
    "df.iloc[:,1:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.9135725473596013"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取特定的值\n",
    "df.iloc[1,3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 布尔值索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用单列的数据作为条件进行筛选\n",
    "df[df.A>0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.712624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.081208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.855027</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2018-11-01       NaN       NaN  0.529903  0.660073\n",
       "2018-11-02       NaN       NaN  0.082960       NaN\n",
       "2018-11-03       NaN  0.730866       NaN  0.712624\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193\n",
       "2018-11-05       NaN       NaN       NaN  0.081208\n",
       "2018-11-06       NaN  0.545561  1.091127       NaN\n",
       "2018-11-07       NaN       NaN       NaN  0.855027"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " #很少用到，很少使用这种大范围的条件进行筛选\n",
    "df[df>0] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>20181101</td>\n",
       "      <td>3</td>\n",
       "      <td>test</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>20181101</td>\n",
       "      <td>3</td>\n",
       "      <td>train</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>20181101</td>\n",
       "      <td>3</td>\n",
       "      <td>test</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>20181101</td>\n",
       "      <td>3</td>\n",
       "      <td>train</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A         B  C      D    E\n",
       "0  1  20181101  3   test  1.5\n",
       "1  1  20181101  3  train  1.5\n",
       "2  1  20181101  3   test  1.5\n",
       "3  1  20181101  3  train  1.5"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用isin()方法过滤\n",
    "df2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>20181101</td>\n",
       "      <td>3</td>\n",
       "      <td>test</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>20181101</td>\n",
       "      <td>3</td>\n",
       "      <td>test</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A         B  C     D    E\n",
       "0  1  20181101  3  test  1.5\n",
       "2  1  20181101  3  test  1.5"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2[df2['D'].isin(['test'])]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 设定数值（类似于sql update 或者add）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 设定一个新的列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['E'] = [1,2,3,4,5,6,7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>-1.913573</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>0.712624</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D  E\n",
       "2018-11-01 -0.170364 -0.237541  0.529903  0.660073  1\n",
       "2018-11-02 -0.158446 -0.488535  0.082960 -1.913573  2\n",
       "2018-11-03 -0.518426  0.730866 -1.033830  0.712624  3\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193  4\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208  5\n",
       "2018-11-06 -0.030580  0.545561  1.091127 -0.131579  6\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027  7"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 通过标签设定新的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.loc['2018-11-01','E']= 10  # 第一行，E列的数据修改为10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>-1.913573</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>0.712624</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D   E\n",
       "2018-11-01 -0.170364 -0.237541  0.529903  0.660073  10\n",
       "2018-11-02 -0.158446 -0.488535  0.082960 -1.913573   2\n",
       "2018-11-03 -0.518426  0.730866 -1.033830  0.712624   3\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193   4\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208   5\n",
       "2018-11-06 -0.030580  0.545561  1.091127 -0.131579   6\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027   7"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.iloc[1,4]=5000  # 第二行第五列数据修改为5000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>-1.913573</td>\n",
       "      <td>5000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>0.712624</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D     E\n",
       "2018-11-01 -0.170364 -0.237541  0.529903  0.660073    10\n",
       "2018-11-02 -0.158446 -0.488535  0.082960 -1.913573  5000\n",
       "2018-11-03 -0.518426  0.730866 -1.033830  0.712624     3\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193     4\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208     5\n",
       "2018-11-06 -0.030580  0.545561  1.091127 -0.131579     6\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027     7"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>0.170364</td>\n",
       "      <td>0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>0.158446</td>\n",
       "      <td>0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>1.913573</td>\n",
       "      <td>5000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>1.033830</td>\n",
       "      <td>0.712624</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>0.897497</td>\n",
       "      <td>0.016279</td>\n",
       "      <td>0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>0.131579</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>0.313342</td>\n",
       "      <td>0.688179</td>\n",
       "      <td>0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D     E\n",
       "2018-11-01  0.170364  0.237541  0.529903  0.660073    10\n",
       "2018-11-02  0.158446  0.488535  0.082960  1.913573  5000\n",
       "2018-11-03  0.518426  0.730866  1.033830  0.712624     3\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193     4\n",
       "2018-11-05  0.897497  0.016279  0.234993  0.081208     5\n",
       "2018-11-06  0.030580  0.545561  1.091127  0.131579     6\n",
       "2018-11-07  0.313342  0.688179  0.417754  0.855027     7"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3 =df.copy()\n",
    "df3[df3<0]= -df3\n",
    "df3  # 都变成非负数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### 缺失值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>0.660073</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>-1.913573</td>\n",
       "      <td>5000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>0.712624</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D     E\n",
       "2018-11-01 -0.170364 -0.237541  0.529903  0.660073    10\n",
       "2018-11-02 -0.158446 -0.488535  0.082960 -1.913573  5000\n",
       "2018-11-03 -0.518426  0.730866 -1.033830  0.712624     3\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193     4\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208     5\n",
       "2018-11-06 -0.030580  0.545561  1.091127 -0.131579     6\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027     7"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['E']=[1,np.nan,2,np.nan,4,np.nan,6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.loc['2018-11-01':'2018-11-03','D']=np.nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D    E\n",
       "2018-11-01 -0.170364 -0.237541  0.529903       NaN  1.0\n",
       "2018-11-02 -0.158446 -0.488535  0.082960       NaN  NaN\n",
       "2018-11-03 -0.518426  0.730866 -1.033830       NaN  2.0\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193  NaN\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208  4.0\n",
       "2018-11-06 -0.030580  0.545561  1.091127 -0.131579  NaN\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027  6.0"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 去掉缺失值的行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "df4 = df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D    E\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208  4.0\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027  6.0"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.dropna(how='any')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D    E\n",
       "2018-11-01 -0.170364 -0.237541  0.529903       NaN  1.0\n",
       "2018-11-02 -0.158446 -0.488535  0.082960       NaN  NaN\n",
       "2018-11-03 -0.518426  0.730866 -1.033830       NaN  2.0\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193  NaN\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208  4.0\n",
       "2018-11-06 -0.030580  0.545561  1.091127 -0.131579  NaN\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027  6.0"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.dropna(how='all')\n",
    "# \"\"\"DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)\"\"\" \n",
    "# aixs 轴0或者1 index或者columns\n",
    "# how 方式\n",
    "# thresh 超过阈值个数的缺失值\n",
    "# subset 那些字段的处理\n",
    "# inplace 是否直接在原数据框中的替换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 对缺失值就行填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>1000.000000</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>1000.000000</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>1000.000000</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C            D       E\n",
       "2018-11-01 -0.170364 -0.237541  0.529903  1000.000000     1.0\n",
       "2018-11-02 -0.158446 -0.488535  0.082960  1000.000000  1000.0\n",
       "2018-11-03 -0.518426  0.730866 -1.033830  1000.000000     2.0\n",
       "2018-11-04  1.013527  0.270167  0.081805     0.178193  1000.0\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993     0.081208     4.0\n",
       "2018-11-06 -0.030580  0.545561  1.091127    -0.131579  1000.0\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754     0.855027     6.0"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.fillna(1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 对数据进行布尔值进行填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                A      B      C      D      E\n",
       "2018-11-01  False  False  False   True  False\n",
       "2018-11-02  False  False  False   True   True\n",
       "2018-11-03  False  False  False   True  False\n",
       "2018-11-04  False  False  False  False   True\n",
       "2018-11-05  False  False  False  False  False\n",
       "2018-11-06  False  False  False  False   True\n",
       "2018-11-07  False  False  False  False  False"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.isnull(df4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 数据操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A   -0.153590\n",
       "B    0.016580\n",
       "C    0.014174\n",
       "D    0.245712\n",
       "E    3.250000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#统计的工作一般情况下都不包含缺失值，\n",
    "df4.mean() \n",
    "#  默认是对列进行求平均，沿着行方向也就是axis=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2018-11-01    0.280499\n",
       "2018-11-02   -0.188007\n",
       "2018-11-03    0.294653\n",
       "2018-11-04    0.385923\n",
       "2018-11-05    0.586488\n",
       "2018-11-06    0.368632\n",
       "2018-11-07    1.087150\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.mean(axis=1)\n",
    "#  沿着列方向求每行的平均"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2018-11-01    1.0\n",
       "2018-11-02    3.0\n",
       "2018-11-03    4.0\n",
       "2018-11-04    NaN\n",
       "2018-11-05    6.0\n",
       "2018-11-06    7.0\n",
       "2018-11-07    8.0\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " # 对于拥有不同维度，需要对齐的对象进行操作。Pandas会自动的沿着指定的维度进行广播：\n",
    "s = pd.Series([1,3,4,np.nan,6,7,8],index=dates)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-1.170364</td>\n",
       "      <td>-1.237541</td>\n",
       "      <td>-0.470097</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-3.158446</td>\n",
       "      <td>-3.488535</td>\n",
       "      <td>-2.917040</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-4.518426</td>\n",
       "      <td>-3.269134</td>\n",
       "      <td>-5.033830</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-6.897497</td>\n",
       "      <td>-6.016279</td>\n",
       "      <td>-6.234993</td>\n",
       "      <td>-5.918792</td>\n",
       "      <td>-2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-7.030580</td>\n",
       "      <td>-6.454439</td>\n",
       "      <td>-5.908873</td>\n",
       "      <td>-7.131579</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-8.313342</td>\n",
       "      <td>-8.688179</td>\n",
       "      <td>-8.417754</td>\n",
       "      <td>-7.144973</td>\n",
       "      <td>-2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D    E\n",
       "2018-11-01 -1.170364 -1.237541 -0.470097       NaN  0.0\n",
       "2018-11-02 -3.158446 -3.488535 -2.917040       NaN  NaN\n",
       "2018-11-03 -4.518426 -3.269134 -5.033830       NaN -2.0\n",
       "2018-11-04       NaN       NaN       NaN       NaN  NaN\n",
       "2018-11-05 -6.897497 -6.016279 -6.234993 -5.918792 -2.0\n",
       "2018-11-06 -7.030580 -6.454439 -5.908873 -7.131579  NaN\n",
       "2018-11-07 -8.313342 -8.688179 -8.417754 -7.144973 -2.0"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.sub(s,axis='index')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.158446</td>\n",
       "      <td>-0.488535</td>\n",
       "      <td>0.082960</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.518426</td>\n",
       "      <td>0.730866</td>\n",
       "      <td>-1.033830</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>1.013527</td>\n",
       "      <td>0.270167</td>\n",
       "      <td>0.081805</td>\n",
       "      <td>0.178193</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.897497</td>\n",
       "      <td>-0.016279</td>\n",
       "      <td>-0.234993</td>\n",
       "      <td>0.081208</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.030580</td>\n",
       "      <td>0.545561</td>\n",
       "      <td>1.091127</td>\n",
       "      <td>-0.131579</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-0.313342</td>\n",
       "      <td>-0.688179</td>\n",
       "      <td>-0.417754</td>\n",
       "      <td>0.855027</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D    E\n",
       "2018-11-01 -0.170364 -0.237541  0.529903       NaN  1.0\n",
       "2018-11-02 -0.158446 -0.488535  0.082960       NaN  NaN\n",
       "2018-11-03 -0.518426  0.730866 -1.033830       NaN  2.0\n",
       "2018-11-04  1.013527  0.270167  0.081805  0.178193  NaN\n",
       "2018-11-05 -0.897497 -0.016279 -0.234993  0.081208  4.0\n",
       "2018-11-06 -0.030580  0.545561  1.091127 -0.131579  NaN\n",
       "2018-11-07 -0.313342 -0.688179 -0.417754  0.855027  6.0"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-11-01</th>\n",
       "      <td>-0.170364</td>\n",
       "      <td>-0.237541</td>\n",
       "      <td>0.529903</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-02</th>\n",
       "      <td>-0.328810</td>\n",
       "      <td>-0.726077</td>\n",
       "      <td>0.612863</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-03</th>\n",
       "      <td>-0.847235</td>\n",
       "      <td>0.004789</td>\n",
       "      <td>-0.420966</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-04</th>\n",
       "      <td>0.166292</td>\n",
       "      <td>0.274956</td>\n",
       "      <td>-0.339161</td>\n",
       "      <td>0.178193</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-05</th>\n",
       "      <td>-0.731205</td>\n",
       "      <td>0.258677</td>\n",
       "      <td>-0.574154</td>\n",
       "      <td>0.259402</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-06</th>\n",
       "      <td>-0.761786</td>\n",
       "      <td>0.804237</td>\n",
       "      <td>0.516973</td>\n",
       "      <td>0.127822</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-11-07</th>\n",
       "      <td>-1.075128</td>\n",
       "      <td>0.116059</td>\n",
       "      <td>0.099219</td>\n",
       "      <td>0.982849</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D     E\n",
       "2018-11-01 -0.170364 -0.237541  0.529903       NaN   1.0\n",
       "2018-11-02 -0.328810 -0.726077  0.612863       NaN   NaN\n",
       "2018-11-03 -0.847235  0.004789 -0.420966       NaN   3.0\n",
       "2018-11-04  0.166292  0.274956 -0.339161  0.178193   NaN\n",
       "2018-11-05 -0.731205  0.258677 -0.574154  0.259402   7.0\n",
       "2018-11-06 -0.761786  0.804237  0.516973  0.127822   NaN\n",
       "2018-11-07 -1.075128  0.116059  0.099219  0.982849  13.0"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.apply(np.cumsum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    1.911024\n",
       "B    1.419044\n",
       "C    2.124957\n",
       "D    0.986606\n",
       "E    5.000000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.apply(lambda x: x.max()-x.min())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 统计个数与离散化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     5\n",
       "1     4\n",
       "2     1\n",
       "3     2\n",
       "4     1\n",
       "5     0\n",
       "6     2\n",
       "7     6\n",
       "8     4\n",
       "9     3\n",
       "10    1\n",
       "11    1\n",
       "12    1\n",
       "13    3\n",
       "14    2\n",
       "dtype: int32"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(np.random.randint(0,7,size=15))\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    5\n",
       "2    3\n",
       "4    2\n",
       "3    2\n",
       "6    1\n",
       "5    1\n",
       "0    1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.value_counts()\n",
    "# 统计元素的个数，并按照元素统计量进行排序，未出现的元素不会显示出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    5\n",
       "1    4\n",
       "2    1\n",
       "3    2\n",
       "4    1\n",
       "5    0\n",
       "6    2\n",
       "dtype: int32"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.reindex(range(0,7))\n",
    "# 按照固定的顺序输出元素的个数统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "dtype: int32"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.mode()\n",
    "#  众数 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 离散化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5, 18, 13, 16, 16,  1, 15, 11,  0, 17, 16, 18, 15, 12, 13])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 连续值转化为离散值，可以使用cut函数进行操作（bins based on vlaues） qcut （bins based on sample\n",
    "# quantiles） 函数\n",
    "arr = np.random.randint(0,20,size=15)  # 正态分布\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(-0.018, 6.0], (12.0, 18.0], (12.0, 18.0], (12.0, 18.0], (12.0, 18.0], ..., (12.0, 18.0], (12.0, 18.0], (12.0, 18.0], (6.0, 12.0], (12.0, 18.0]]\n",
       "Length: 15\n",
       "Categories (3, interval[float64]): [(-0.018, 6.0] < (6.0, 12.0] < (12.0, 18.0]]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor = pd.cut(arr,3)\n",
    "factor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12.0, 18.0]     10\n",
       "(-0.018, 6.0]     3\n",
       "(6.0, 12.0]       2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.value_counts(factor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "factor1 = pd.cut(arr,[-1,5,10,15,20])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(15, 20]    6\n",
       "(10, 15]    6\n",
       "(-1, 5]     3\n",
       "(5, 10]     0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.value_counts(factor1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "factor2 = pd.qcut(arr,[0,0.25,0.5,0.75,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(11.5, 15.0]      5\n",
       "(-0.001, 11.5]    4\n",
       "(16.0, 18.0]      3\n",
       "(15.0, 16.0]      3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.value_counts(factor2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### pandas 处理字符串（单独一个大的章节，这人不做详述）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据合并"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- concat\n",
    "- merge（类似于sql数据库中的join）\n",
    "- append"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 首先看concat合并数据框"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.949746</td>\n",
       "      <td>-0.050767</td>\n",
       "      <td>1.478622</td>\n",
       "      <td>-0.239901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.297120</td>\n",
       "      <td>-0.562589</td>\n",
       "      <td>0.371837</td>\n",
       "      <td>1.180715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.953856</td>\n",
       "      <td>0.492295</td>\n",
       "      <td>0.821156</td>\n",
       "      <td>-0.323328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.016153</td>\n",
       "      <td>1.554225</td>\n",
       "      <td>-1.166304</td>\n",
       "      <td>-0.904040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.204763</td>\n",
       "      <td>-0.951291</td>\n",
       "      <td>-1.317620</td>\n",
       "      <td>0.672900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2.241006</td>\n",
       "      <td>-0.925746</td>\n",
       "      <td>-1.961408</td>\n",
       "      <td>0.853367</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2.217133</td>\n",
       "      <td>-0.430812</td>\n",
       "      <td>0.518926</td>\n",
       "      <td>1.741445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.571104</td>\n",
       "      <td>-0.437305</td>\n",
       "      <td>-0.902241</td>\n",
       "      <td>0.786231</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-2.511387</td>\n",
       "      <td>0.523760</td>\n",
       "      <td>1.811622</td>\n",
       "      <td>-0.777296</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.252690</td>\n",
       "      <td>0.901952</td>\n",
       "      <td>0.619614</td>\n",
       "      <td>-0.006631</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3\n",
       "0  0.949746 -0.050767  1.478622 -0.239901\n",
       "1 -0.297120 -0.562589  0.371837  1.180715\n",
       "2  0.953856  0.492295  0.821156 -0.323328\n",
       "3  0.016153  1.554225 -1.166304 -0.904040\n",
       "4  0.204763 -0.951291 -1.317620  0.672900\n",
       "5  2.241006 -0.925746 -1.961408  0.853367\n",
       "6  2.217133 -0.430812  0.518926  1.741445\n",
       "7 -0.571104 -0.437305 -0.902241  0.786231\n",
       "8 -2.511387  0.523760  1.811622 -0.777296\n",
       "9  0.252690  0.901952  0.619614 -0.006631"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(10,4))  #  10行列的标准正态分布数据框\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(          0         1         2         3\n",
       " 0  0.949746 -0.050767  1.478622 -0.239901\n",
       " 1 -0.297120 -0.562589  0.371837  1.180715\n",
       " 2  0.953856  0.492295  0.821156 -0.323328,\n",
       "           0         1         2         3\n",
       " 3  0.016153  1.554225 -1.166304 -0.904040\n",
       " 4  0.204763 -0.951291 -1.317620  0.672900\n",
       " 5  2.241006 -0.925746 -1.961408  0.853367\n",
       " 6  2.217133 -0.430812  0.518926  1.741445,\n",
       "           0         1         2         3\n",
       " 7 -0.571104 -0.437305 -0.902241  0.786231\n",
       " 8 -2.511387  0.523760  1.811622 -0.777296\n",
       " 9  0.252690  0.901952  0.619614 -0.006631)"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1,d2,d3  = df[:3],df[3:7],df[7:]\n",
    "d1,d2,d3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.949746</td>\n",
       "      <td>-0.050767</td>\n",
       "      <td>1.478622</td>\n",
       "      <td>-0.239901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.297120</td>\n",
       "      <td>-0.562589</td>\n",
       "      <td>0.371837</td>\n",
       "      <td>1.180715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.953856</td>\n",
       "      <td>0.492295</td>\n",
       "      <td>0.821156</td>\n",
       "      <td>-0.323328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.016153</td>\n",
       "      <td>1.554225</td>\n",
       "      <td>-1.166304</td>\n",
       "      <td>-0.904040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.204763</td>\n",
       "      <td>-0.951291</td>\n",
       "      <td>-1.317620</td>\n",
       "      <td>0.672900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2.241006</td>\n",
       "      <td>-0.925746</td>\n",
       "      <td>-1.961408</td>\n",
       "      <td>0.853367</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2.217133</td>\n",
       "      <td>-0.430812</td>\n",
       "      <td>0.518926</td>\n",
       "      <td>1.741445</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.571104</td>\n",
       "      <td>-0.437305</td>\n",
       "      <td>-0.902241</td>\n",
       "      <td>0.786231</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-2.511387</td>\n",
       "      <td>0.523760</td>\n",
       "      <td>1.811622</td>\n",
       "      <td>-0.777296</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.252690</td>\n",
       "      <td>0.901952</td>\n",
       "      <td>0.619614</td>\n",
       "      <td>-0.006631</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3\n",
       "0  0.949746 -0.050767  1.478622 -0.239901\n",
       "1 -0.297120 -0.562589  0.371837  1.180715\n",
       "2  0.953856  0.492295  0.821156 -0.323328\n",
       "3  0.016153  1.554225 -1.166304 -0.904040\n",
       "4  0.204763 -0.951291 -1.317620  0.672900\n",
       "5  2.241006 -0.925746 -1.961408  0.853367\n",
       "6  2.217133 -0.430812  0.518926  1.741445\n",
       "7 -0.571104 -0.437305 -0.902241  0.786231\n",
       "8 -2.511387  0.523760  1.811622 -0.777296\n",
       "9  0.252690  0.901952  0.619614 -0.006631"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([d1,d2,d3])\n",
    "#合并三个数据框，数据结构相同，通常合并相同结构的数据，数据框中的字段一致，类似于数据添加新的数据来源"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### merge方式合并（数据库中的join）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "left = pd.DataFrame({'key':['foo','foo'],\"lval\":[1,2]})\n",
    "right = pd.DataFrame({'key':['foo','foo'],'rval':[4,5]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>lval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>foo</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   key  lval\n",
       "0  foo     1\n",
       "1  foo     2"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>rval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>foo</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   key  rval\n",
       "0  foo     4\n",
       "1  foo     5"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "right"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>lval</th>\n",
       "      <th>rval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>foo</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>foo</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   key  lval  rval\n",
       "0  foo     1     4\n",
       "1  foo     1     5\n",
       "2  foo     2     4\n",
       "3  foo     2     5"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(left,right,on='key')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>lval</th>\n",
       "      <th>rval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bar</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   key  lval  rval\n",
       "0  foo     1     4\n",
       "1  bar     2     5"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left = pd.DataFrame({'key':['foo','bar'],\"lval\":[1,2]})\n",
    "right = pd.DataFrame({'key':['foo','bar'],'rval':[4,5]})\n",
    "pd.merge(left,right,on='key')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>lval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bar</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   key  lval\n",
       "0  foo     1\n",
       "1  bar     2"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "left"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>rval</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bar</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   key  rval\n",
       "0  foo     4\n",
       "1  bar     5"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "right"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Append方式合并数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.825997</td>\n",
       "      <td>-0.331086</td>\n",
       "      <td>-0.067143</td>\n",
       "      <td>0.747226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.027497</td>\n",
       "      <td>0.861639</td>\n",
       "      <td>0.928621</td>\n",
       "      <td>-2.549617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.546645</td>\n",
       "      <td>-0.072253</td>\n",
       "      <td>-0.788483</td>\n",
       "      <td>0.484140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.472240</td>\n",
       "      <td>-1.776993</td>\n",
       "      <td>-1.647407</td>\n",
       "      <td>0.170596</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.099453</td>\n",
       "      <td>0.380143</td>\n",
       "      <td>-0.890510</td>\n",
       "      <td>1.233741</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.351915</td>\n",
       "      <td>0.137522</td>\n",
       "      <td>-1.165938</td>\n",
       "      <td>1.128146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.558442</td>\n",
       "      <td>-1.047060</td>\n",
       "      <td>-0.598197</td>\n",
       "      <td>-1.979876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.067321</td>\n",
       "      <td>-1.037666</td>\n",
       "      <td>-1.140675</td>\n",
       "      <td>-0.098562</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D\n",
       "0  1.825997 -0.331086 -0.067143  0.747226\n",
       "1 -0.027497  0.861639  0.928621 -2.549617\n",
       "2 -0.546645 -0.072253 -0.788483  0.484140\n",
       "3 -0.472240 -1.776993 -1.647407  0.170596\n",
       "4 -0.099453  0.380143 -0.890510  1.233741\n",
       "5  0.351915  0.137522 -1.165938  1.128146\n",
       "6  0.558442 -1.047060 -0.598197 -1.979876\n",
       "7  0.067321 -1.037666 -1.140675 -0.098562"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  与concat 类似，常用的方法可以参考一下日子\n",
    "df = pd.DataFrame(np.random.randn(8,4),columns=['A','B','C','D'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.825997</td>\n",
       "      <td>-0.331086</td>\n",
       "      <td>-0.067143</td>\n",
       "      <td>0.747226</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.027497</td>\n",
       "      <td>0.861639</td>\n",
       "      <td>0.928621</td>\n",
       "      <td>-2.549617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.546645</td>\n",
       "      <td>-0.072253</td>\n",
       "      <td>-0.788483</td>\n",
       "      <td>0.484140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.472240</td>\n",
       "      <td>-1.776993</td>\n",
       "      <td>-1.647407</td>\n",
       "      <td>0.170596</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.099453</td>\n",
       "      <td>0.380143</td>\n",
       "      <td>-0.890510</td>\n",
       "      <td>1.233741</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.351915</td>\n",
       "      <td>0.137522</td>\n",
       "      <td>-1.165938</td>\n",
       "      <td>1.128146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.558442</td>\n",
       "      <td>-1.047060</td>\n",
       "      <td>-0.598197</td>\n",
       "      <td>-1.979876</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.067321</td>\n",
       "      <td>-1.037666</td>\n",
       "      <td>-1.140675</td>\n",
       "      <td>-0.098562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-0.472240</td>\n",
       "      <td>-1.776993</td>\n",
       "      <td>-1.647407</td>\n",
       "      <td>0.170596</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D\n",
       "0  1.825997 -0.331086 -0.067143  0.747226\n",
       "1 -0.027497  0.861639  0.928621 -2.549617\n",
       "2 -0.546645 -0.072253 -0.788483  0.484140\n",
       "3 -0.472240 -1.776993 -1.647407  0.170596\n",
       "4 -0.099453  0.380143 -0.890510  1.233741\n",
       "5  0.351915  0.137522 -1.165938  1.128146\n",
       "6  0.558442 -1.047060 -0.598197 -1.979876\n",
       "7  0.067321 -1.037666 -1.140675 -0.098562\n",
       "8 -0.472240 -1.776993 -1.647407  0.170596"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## \n",
    "d1 = df.iloc[3]\n",
    "df.append(d1,ignore_index= True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 分组操作Groupby操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foo</td>\n",
       "      <td>one</td>\n",
       "      <td>0.938910</td>\n",
       "      <td>0.505163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bar</td>\n",
       "      <td>one</td>\n",
       "      <td>0.660543</td>\n",
       "      <td>0.353860</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>foo</td>\n",
       "      <td>two</td>\n",
       "      <td>0.520309</td>\n",
       "      <td>1.157462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>bar</td>\n",
       "      <td>three</td>\n",
       "      <td>-1.054927</td>\n",
       "      <td>0.290693</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     A      B         C         D\n",
       "0  foo    one  0.938910  0.505163\n",
       "1  bar    one  0.660543  0.353860\n",
       "2  foo    two  0.520309  1.157462\n",
       "3  bar  three -1.054927  0.290693"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\"A\":['foo','bar','foo','bar'],\n",
    "                  \"B\":['one','one','two','three'],\n",
    "                  \"C\":np.random.randn(4),\n",
    "                  \"D\":np.random.randn(4)})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>bar</th>\n",
       "      <td>-0.394384</td>\n",
       "      <td>0.644553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>foo</th>\n",
       "      <td>1.459219</td>\n",
       "      <td>1.662625</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            C         D\n",
       "A                      \n",
       "bar -0.394384  0.644553\n",
       "foo  1.459219  1.662625"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('A').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A\n",
       "bar    2\n",
       "foo    2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby('A').size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>0.660543</td>\n",
       "      <td>0.353860</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>-1.054927</td>\n",
       "      <td>0.290693</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>0.938910</td>\n",
       "      <td>0.505163</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.520309</td>\n",
       "      <td>1.157462</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  C         D\n",
       "A   B                        \n",
       "bar one    0.660543  0.353860\n",
       "    three -1.054927  0.290693\n",
       "foo one    0.938910  0.505163\n",
       "    two    0.520309  1.157462"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(['A',\"B\"]).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    B    \n",
       "bar  one      1\n",
       "     three    1\n",
       "foo  one      1\n",
       "     two      1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(['A',\"B\"]).size()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### reshape操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [],
   "source": [
    "tuples = list(zip(*[['bar','bar','baz','baz','foo','foo','qux','qux'],\n",
    "                   ['one','two','one','two','one','two','one','two']]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "index = pd.MultiIndex.from_tuples(tuples,names=['first','second'])\n",
    "df = pd.DataFrame(np.random.randn(8,2),index= index,columns=['A','B'])\n",
    "df2 =  df[:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>0.510758</td>\n",
       "      <td>0.641370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.481230</td>\n",
       "      <td>-0.470894</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.076294</td>\n",
       "      <td>0.121247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.378507</td>\n",
       "      <td>-1.358932</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     A         B\n",
       "first second                    \n",
       "bar   one     0.510758  0.641370\n",
       "      two     0.481230 -0.470894\n",
       "baz   one    -0.076294  0.121247\n",
       "      two     0.378507 -1.358932"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>0.510758</td>\n",
       "      <td>0.641370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.481230</td>\n",
       "      <td>-0.470894</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.076294</td>\n",
       "      <td>0.121247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.378507</td>\n",
       "      <td>-1.358932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">foo</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.873012</td>\n",
       "      <td>0.531595</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.266968</td>\n",
       "      <td>-0.393124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">qux</th>\n",
       "      <th>one</th>\n",
       "      <td>0.981866</td>\n",
       "      <td>1.205994</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.265772</td>\n",
       "      <td>0.132489</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     A         B\n",
       "first second                    \n",
       "bar   one     0.510758  0.641370\n",
       "      two     0.481230 -0.470894\n",
       "baz   one    -0.076294  0.121247\n",
       "      two     0.378507 -1.358932\n",
       "foo   one    -0.873012  0.531595\n",
       "      two     0.266968 -0.393124\n",
       "qux   one     0.981866  1.205994\n",
       "      two     0.265772  0.132489"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "####  stack 与unstack 方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2_stacked = df2.stack()  \n",
    "#  将column也作为index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "first  second   \n",
       "bar    one     A    0.510758\n",
       "               B    0.641370\n",
       "       two     A    0.481230\n",
       "               B   -0.470894\n",
       "baz    one     A   -0.076294\n",
       "               B    0.121247\n",
       "       two     A    0.378507\n",
       "               B   -1.358932\n",
       "dtype: float64"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2_stacked"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>one</th>\n",
       "      <td>0.510758</td>\n",
       "      <td>0.641370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.481230</td>\n",
       "      <td>-0.470894</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>one</th>\n",
       "      <td>-0.076294</td>\n",
       "      <td>0.121247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.378507</td>\n",
       "      <td>-1.358932</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     A         B\n",
       "first second                    \n",
       "bar   one     0.510758  0.641370\n",
       "      two     0.481230 -0.470894\n",
       "baz   one    -0.076294  0.121247\n",
       "      two     0.378507 -1.358932"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2_stacked.unstack()  #  回复到原来的状态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "first  second   \n",
       "bar    one     A    0.510758\n",
       "               B    0.641370\n",
       "       two     A    0.481230\n",
       "               B   -0.470894\n",
       "baz    one     A   -0.076294\n",
       "               B    0.121247\n",
       "       two     A    0.378507\n",
       "               B   -1.358932\n",
       "dtype: float64"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2_stacked"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>second</th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>first</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">bar</th>\n",
       "      <th>A</th>\n",
       "      <td>0.510758</td>\n",
       "      <td>0.481230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>0.641370</td>\n",
       "      <td>-0.470894</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">baz</th>\n",
       "      <th>A</th>\n",
       "      <td>-0.076294</td>\n",
       "      <td>0.378507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>0.121247</td>\n",
       "      <td>-1.358932</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "second        one       two\n",
       "first                      \n",
       "bar   A  0.510758  0.481230\n",
       "      B  0.641370 -0.470894\n",
       "baz   A -0.076294  0.378507\n",
       "      B  0.121247 -1.358932"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2_stacked.unstack(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>first</th>\n",
       "      <th>bar</th>\n",
       "      <th>baz</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>second</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">one</th>\n",
       "      <th>A</th>\n",
       "      <td>0.510758</td>\n",
       "      <td>-0.076294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>0.641370</td>\n",
       "      <td>0.121247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">two</th>\n",
       "      <th>A</th>\n",
       "      <td>0.481230</td>\n",
       "      <td>0.378507</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.470894</td>\n",
       "      <td>-1.358932</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "first          bar       baz\n",
       "second                      \n",
       "one    A  0.510758 -0.076294\n",
       "       B  0.641370  0.121247\n",
       "two    A  0.481230  0.378507\n",
       "       B -0.470894 -1.358932"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2_stacked.unstack(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pivot_table 透视表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({'A' : ['one', 'one', 'two', 'three'] * 3,                    'B' : ['A', 'B', 'C'] * 4,\n",
    "                 'C' : ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 2,\n",
    "                  'D' : np.random.randn(12),\n",
    "                 'E' : np.random.randn(12)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>one</td>\n",
       "      <td>A</td>\n",
       "      <td>foo</td>\n",
       "      <td>0.006247</td>\n",
       "      <td>-0.894827</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>one</td>\n",
       "      <td>B</td>\n",
       "      <td>foo</td>\n",
       "      <td>1.653974</td>\n",
       "      <td>-0.340107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>two</td>\n",
       "      <td>C</td>\n",
       "      <td>foo</td>\n",
       "      <td>-1.627485</td>\n",
       "      <td>-1.011403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>three</td>\n",
       "      <td>A</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.716002</td>\n",
       "      <td>1.533422</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>one</td>\n",
       "      <td>B</td>\n",
       "      <td>bar</td>\n",
       "      <td>0.422688</td>\n",
       "      <td>-0.807675</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>one</td>\n",
       "      <td>C</td>\n",
       "      <td>bar</td>\n",
       "      <td>0.264818</td>\n",
       "      <td>0.249770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>two</td>\n",
       "      <td>A</td>\n",
       "      <td>foo</td>\n",
       "      <td>0.643288</td>\n",
       "      <td>-1.166616</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>three</td>\n",
       "      <td>B</td>\n",
       "      <td>foo</td>\n",
       "      <td>0.348041</td>\n",
       "      <td>-0.659099</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>one</td>\n",
       "      <td>C</td>\n",
       "      <td>foo</td>\n",
       "      <td>1.593486</td>\n",
       "      <td>-1.098731</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>one</td>\n",
       "      <td>A</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.389344</td>\n",
       "      <td>0.919528</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>two</td>\n",
       "      <td>B</td>\n",
       "      <td>bar</td>\n",
       "      <td>-1.407450</td>\n",
       "      <td>1.269716</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>three</td>\n",
       "      <td>C</td>\n",
       "      <td>bar</td>\n",
       "      <td>-0.172672</td>\n",
       "      <td>0.883970</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        A  B    C         D         E\n",
       "0     one  A  foo  0.006247 -0.894827\n",
       "1     one  B  foo  1.653974 -0.340107\n",
       "2     two  C  foo -1.627485 -1.011403\n",
       "3   three  A  bar -0.716002  1.533422\n",
       "4     one  B  bar  0.422688 -0.807675\n",
       "5     one  C  bar  0.264818  0.249770\n",
       "6     two  A  foo  0.643288 -1.166616\n",
       "7   three  B  foo  0.348041 -0.659099\n",
       "8     one  C  foo  1.593486 -1.098731\n",
       "9     one  A  bar -0.389344  0.919528\n",
       "10    two  B  bar -1.407450  1.269716\n",
       "11  three  C  bar -0.172672  0.883970"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>bar</th>\n",
       "      <th>foo</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">one</th>\n",
       "      <th>A</th>\n",
       "      <td>-0.389344</td>\n",
       "      <td>0.006247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>0.422688</td>\n",
       "      <td>1.653974</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>0.264818</td>\n",
       "      <td>1.593486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">three</th>\n",
       "      <th>A</th>\n",
       "      <td>-0.716002</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.348041</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-0.172672</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">two</th>\n",
       "      <th>A</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.643288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-1.407450</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-1.627485</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "C             bar       foo\n",
       "A     B                    \n",
       "one   A -0.389344  0.006247\n",
       "      B  0.422688  1.653974\n",
       "      C  0.264818  1.593486\n",
       "three A -0.716002       NaN\n",
       "      B       NaN  0.348041\n",
       "      C -0.172672       NaN\n",
       "two   A       NaN  0.643288\n",
       "      B -1.407450       NaN\n",
       "      C       NaN -1.627485"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.pivot_table(df,values='D',index=['A','B'],columns=['C'],aggfunc=np.mean)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>bar</th>\n",
       "      <th>foo</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">one</th>\n",
       "      <th>A</th>\n",
       "      <td>-0.389344</td>\n",
       "      <td>0.006247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>0.422688</td>\n",
       "      <td>1.653974</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>0.264818</td>\n",
       "      <td>1.593486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">three</th>\n",
       "      <th>A</th>\n",
       "      <td>-0.716002</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.348041</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-0.172672</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">two</th>\n",
       "      <th>A</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.643288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-1.407450</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-1.627485</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "C             bar       foo\n",
       "A     B                    \n",
       "one   A -0.389344  0.006247\n",
       "      B  0.422688  1.653974\n",
       "      C  0.264818  1.593486\n",
       "three A -0.716002       NaN\n",
       "      B       NaN  0.348041\n",
       "      C -0.172672       NaN\n",
       "two   A       NaN  0.643288\n",
       "      B -1.407450       NaN\n",
       "      C       NaN -1.627485"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.pivot_table(df,values='D',index=['A','B'],columns=['C'],aggfunc=np.sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>C</th>\n",
       "      <th>bar</th>\n",
       "      <th>foo</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">one</th>\n",
       "      <th>A</th>\n",
       "      <td>-0.389344</td>\n",
       "      <td>0.006247</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>0.422688</td>\n",
       "      <td>1.653974</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>0.264818</td>\n",
       "      <td>1.593486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">three</th>\n",
       "      <th>A</th>\n",
       "      <td>-0.716002</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.348041</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-0.172672</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">two</th>\n",
       "      <th>A</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.643288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-1.407450</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>-1.627485</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "C             bar       foo\n",
       "A     B                    \n",
       "one   A -0.389344  0.006247\n",
       "      B  0.422688  1.653974\n",
       "      C  0.264818  1.593486\n",
       "three A -0.716002  0.000000\n",
       "      B  0.000000  0.348041\n",
       "      C -0.172672  0.000000\n",
       "two   A  0.000000  0.643288\n",
       "      B -1.407450  0.000000\n",
       "      C  0.000000 -1.627485"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.pivot_table(df,values='D',index=['A','B'],columns=['C'],aggfunc=np.mean,fill_value=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = pd.pivot_table(df,values='D',index=['A','B'],columns=['C'],aggfunc=np.mean,fill_value=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiIndex(levels=[['one', 'three', 'two'], ['A', 'B', 'C']],\n",
       "           labels=[[0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2]],\n",
       "           names=['A', 'B'])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A      B  C  \n",
       "one    A  bar   -0.389344\n",
       "          foo    0.006247\n",
       "       B  bar    0.422688\n",
       "          foo    1.653974\n",
       "       C  bar    0.264818\n",
       "          foo    1.593486\n",
       "three  A  bar   -0.716002\n",
       "          foo    0.000000\n",
       "       B  bar    0.000000\n",
       "          foo    0.348041\n",
       "       C  bar   -0.172672\n",
       "          foo    0.000000\n",
       "two    A  bar    0.000000\n",
       "          foo    0.643288\n",
       "       B  bar   -1.407450\n",
       "          foo    0.000000\n",
       "       C  bar    0.000000\n",
       "          foo   -1.627485\n",
       "dtype: float64"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.stack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <th colspan=\"3\" halign=\"left\">bar</th>\n",
       "      <th colspan=\"3\" halign=\"left\">foo</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>-0.389344</td>\n",
       "      <td>0.422688</td>\n",
       "      <td>0.264818</td>\n",
       "      <td>0.006247</td>\n",
       "      <td>1.653974</td>\n",
       "      <td>1.593486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>-0.716002</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.172672</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.348041</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>-1.407450</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.643288</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-1.627485</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "C           bar                           foo                    \n",
       "B             A         B         C         A         B         C\n",
       "A                                                                \n",
       "one   -0.389344  0.422688  0.264818  0.006247  1.653974  1.593486\n",
       "three -0.716002  0.000000 -0.172672  0.000000  0.348041  0.000000\n",
       "two    0.000000 -1.407450  0.000000  0.643288  0.000000 -1.627485"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.unstack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <th colspan=\"3\" halign=\"left\">bar</th>\n",
       "      <th colspan=\"3\" halign=\"left\">foo</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>one</th>\n",
       "      <td>-0.389344</td>\n",
       "      <td>0.422688</td>\n",
       "      <td>0.264818</td>\n",
       "      <td>0.006247</td>\n",
       "      <td>1.653974</td>\n",
       "      <td>1.593486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>three</th>\n",
       "      <td>-0.716002</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.172672</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.348041</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>two</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>-1.407450</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.643288</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-1.627485</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "C           bar                           foo                    \n",
       "B             A         B         C         A         B         C\n",
       "A                                                                \n",
       "one   -0.389344  0.422688  0.264818  0.006247  1.653974  1.593486\n",
       "three -0.716002  0.000000 -0.172672  0.000000  0.348041  0.000000\n",
       "two    0.000000 -1.407450  0.000000  0.643288  0.000000 -1.627485"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.unstack(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <th colspan=\"3\" halign=\"left\">bar</th>\n",
       "      <th colspan=\"3\" halign=\"left\">foo</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th>one</th>\n",
       "      <th>three</th>\n",
       "      <th>two</th>\n",
       "      <th>one</th>\n",
       "      <th>three</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>-0.389344</td>\n",
       "      <td>-0.716002</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>0.006247</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.643288</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>0.422688</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-1.40745</td>\n",
       "      <td>1.653974</td>\n",
       "      <td>0.348041</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>0.264818</td>\n",
       "      <td>-0.172672</td>\n",
       "      <td>0.00000</td>\n",
       "      <td>1.593486</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-1.627485</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "C       bar                          foo                    \n",
       "A       one     three      two       one     three       two\n",
       "B                                                           \n",
       "A -0.389344 -0.716002  0.00000  0.006247  0.000000  0.643288\n",
       "B  0.422688  0.000000 -1.40745  1.653974  0.348041  0.000000\n",
       "C  0.264818 -0.172672  0.00000  1.593486  0.000000 -1.627485"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.unstack(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 至此，pandas的基础的使用介绍也就结束了，后续会有专题性质的分析，包括(字符串处理，apply的使用，数据合并，透视表，时间序列的分析）"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "203.797px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
